#include "rgbyuv.h"
#include "my_types.h"

void rgbyuv_init() {}

void rgbyuv_run(fifo<D_PIXEL> *b1R,   fifo<D_PIXEL> *b1G,   fifo<D_PIXEL> *b1B,
		fifo<D_PIXEL> *rgbY,  fifo<D_PIXEL> *rgbU,  fifo<D_PIXEL> *rgbV) {

  D_PIXEL  r, g, b;
  D_PIXEL y, u, v;

  D_RGBCOEFF coeff[] = { 0.299,   0.587,   0.114,
                        -0.1687, -0.3313,  0.5,
                         0.5,    -0.4187, -0.0813};

  while (!b1R->empty()) {
    r = b1R->read();
    g = b1G->read();
    b = b1B->read();
	
  y =  (coeff[0] * r + coeff[1] * g + coeff[2] * b);
  u =  (coeff[3] * r + coeff[4] * g + coeff[5] * b) + 128;
  v =  (coeff[6] * r + coeff[7] * g + coeff[8] * b) + 128;


    rgbY->write(y);
    rgbU->write(u);
    rgbV->write(v);
  }
}
